/*******************************************************************************
* Long-term effects of weather-induced migration on urban labor and housing 
  markets
* Busso & Chauvin

* Purpose: Perform regressions at the rural-area-level.

*******************************************************************************/
/***************************************************************/
/* 1. Variables definitions */
/***************************************************************/ 
/* Main controls set */ 
global controls imf_sh isv_sh igv_sh sh_emp_col pop_growth8091

/* Macroregion fixed effects */ 
global feff city_macrofe_1 city_macrofe_2 city_macrofe_3 city_macrofe_4 city_macrofe_5

/* Years */ 
global years 9110 0010

/***************************************************************/
/* 2. Regressions at the rural-area-level - Borusyak */
/***************************************************************/
/* Run city to rural-area transformation */
foreach i in $years {
	
	if "`i'" == "9110" local cond = "1991"
	if "`i'" == "0010" local cond = "2000"	

	* Variables that have the same number of obs. go in the same global
	global outcomes1_`i' d_l_wageres2_r_`i' d_l_emp_r_`i' ///
	d_l_wageres2_r_isv_`i' d_l_emp_r_isv_`i' d_l_wageres2_r_nohs_`i' ///
	d_l_emp_r_nohs_`i' d_l_wageres2_r_hs_`i' d_l_emp_r_hs_`i' 
	global outcomes2_`i' d_l_wageres2_r_imf_`i' d_l_emp_r_imf_`i' 
	global outcomes3_`i' d_l_wageres2_g_`i' d_l_emp_g_`i' 
	global outcomes4_9110 d_rate_part_r_9110 d_imf_sh_r_9110 d_isv_sh_r_9110 ///
	d_rate_inform_r_9110 d_rate_inform2_r_9110 d_hs_r_9110
	global outcomes5_9110 d_l_rentres2_r_9110 d_l_houses_r_9110 d_l_rooms_r_9110
	global outcomes6_9110 d_l_rentres2_l_r_9110 d_l_houses_l_r_9110 d_l_rooms_l_r_9110
	global outcomes7_9110 d_l_rentres2_m_r_9110 d_l_houses_m_r_9110 d_l_rooms_m_r_9110
	global outcomes8_9110 d_l_rentres2_h_r_9110 d_l_houses_h_r_9110 d_l_rooms_h_r_9110
	global outcomes9_9110 d_l_rentres2_g_9110 d_l_houses_g_9110 d_l_rooms_g_9110
	global outcomes10_9110 d_own_all_9110 d_own_all_m_9110 d_own_residents_9110 ///
	d_own_residents_m_9110
	global outcomes11_9110 d_own_all_l_9110
	global outcomes12_9110 d_own_all_h_9110
	global outcomes13_9110 d_own_residents_l_9110  
	global outcomes14_9110 d_own_residents_h_9110
	global outcomes15_9110 d_cityfe_l_wage_r_9110 d_cityfe_l_wage_r_imf_9110 ///
	d_cityfe_l_wage_r_isv_9110
	global outcomes16_9110 d_cityfe_l_wage_r_hs_9110 d_cityfe_l_wage_r_nohs_9110
	global outcomes17_9110 d_cityfe_l_wage_g_9110
	global outcomes18_9110 d_cityfe_l_rent_r_9110 
	global outcomes19_9110 d_cityfe_l_rent_l_rr_9110
	global outcomes20_9110 d_cityfe_l_rent_m_rr_9110
	global outcomes21_9110 d_cityfe_l_rent_h_rr_9110
	global outcomes22_9110 d_cityfe_l_rent_g_9110
	global outcomes23_9110 d_l_rentres2_9110 d_l_houses_9110 d_l_rooms_9110
	global outcomes24_9110 d_l_rentres2_l_9110 d_l_houses_l_9110 d_l_rooms_l_9110
	global outcomes25_9110 d_l_rentres2_m_9110 d_l_houses_m_9110 d_l_rooms_m_9110
	global outcomes26_9110 d_l_rentres2_h_9110 d_l_houses_h_9110 d_l_rooms_h_9110
	global outcomes27_9110 d_l_rentres2_l_g_9110 d_l_houses_l_g_9110 d_l_rooms_l_g_9110
	global outcomes28_9110 d_l_rentres2_m_g_9110 d_l_houses_m_g_9110 d_l_rooms_m_g_9110
	global outcomes29_9110 d_l_rentres2_h_g_9110 d_l_houses_h_g_9110 d_l_rooms_h_g_9110
	
	forval j = 1/29 {
		dis "${outcomes`j'_`i'}"
		use $db_out/shift_share_db_cities.dta, clear
		keep if year == `cond'
		
		sum rate_mig_0110_rural 
		local obs = r(N)
		local dep_mean = r(mean)

		/* Transform to shock-level variables */ 
		ssaggregate ${outcomes`j'_`i'} rate_mig_0110_rural shift_0009_dws_t_rur ///
		shift_0009_dws_t_rur_abs, ///
		n(mca8010) s(city_rur_mig_91_sh) ///
		l(mcarp8010) sfile($db_out/shift_share_db_shares.dta) ///
		controls("$feff" "$controls $feff" "$controls $feff l_pop_all91" ///
		"$controls $feff l_pop_all91 pop_growth0010" ///
		"$controls $feff l_pop_all91 comtime_10" ///
		"$controls $feff l_pop_all91 shift_8190_dws_t_rur" ///
		"$controls $feff l_pop_all91 comtime_10_l" ///
		"$controls $feff l_pop_all91 comtime_10_m" ///
		"$controls $feff l_pop_all91 comtime_10_h")    

		/* Bring-in the droughts variable */ 
		merge 1:1 mca8010 using $db_out/shift_share_db_rural.dta
		keep if _merge == 3
		drop _merge
		merge 1:1 mca8010 using $db_out/cluster_vars
		keep if _merge == 3
		drop _merge
		
		/* SPEI absolute value variable */
		gen abs_spei = abs(drought_dws0009)

		save $da_tmp/shockstransf_outcomes`j'_cityfe_`i', replace
	}
}

/***************************************************************/
/* 2.1 Run first stage with city-level transformation */
/***************************************************************/
foreach x in "drought_dws0009" "abs_spei" { 
	if "`x'" == "drought_dws0009" local cond = ""
	if "`x'" == "abs_spei" local cond = "_abs"
	
	use $da_tmp/shockstransf_outcomes1_cityfe_9110, clear

	mat define tmat = J(5, 6, .)

	qui ivreg rate_mig_0110_rural1 (shift_0009_dws_t_rur`cond'1 = `x') [aw=s_n], cluster(mcm8010)
	
	local F1 = (_b[shift_0009_dws_t_rur`cond'1]/_se[shift_0009_dws_t_rur`cond'1])^2
	matrix tmat[1, 1] = r(table)[1, 1]
	matrix tmat[2, 1] = r(table)[2, 1]
	matrix tmat[1, 2] = r(table)[4, 1]
	matrix tmat[3, 1] = `F1'
	matrix tmat[4, 1] = `obs'
	matrix tmat[5, 1] = `dep_mean'

	qui ivreg rate_mig_0110_rural2 (shift_0009_dws_t_rur`cond'2 = `x') [aw=s_n], cluster(mcm8010)
	local F1 = (_b[shift_0009_dws_t_rur`cond'2]/_se[shift_0009_dws_t_rur`cond'2])^2
	matrix tmat[1, 3] = r(table)[1, 1]
	matrix tmat[2, 3] = r(table)[2, 1]
	matrix tmat[1, 4] = r(table)[4, 1]
	matrix tmat[3, 3] = `F1'
	matrix tmat[4, 3] = `obs'
	matrix tmat[5, 3] = `dep_mean'

	qui ivreg rate_mig_0110_rural3 (shift_0009_dws_t_rur`cond'3 = `x') [aw=s_n], cluster(mcm8010)
	local F1 = (_b[shift_0009_dws_t_rur`cond'3]/_se[shift_0009_dws_t_rur`cond'3])^2	
	matrix tmat[1, 5] = r(table)[1, 1]
	matrix tmat[2, 5] = r(table)[2, 1]
	matrix tmat[1, 6] = r(table)[4, 1]
	matrix tmat[3, 5] = `F1'
	matrix tmat[4, 5] = `obs'
	matrix tmat[5, 5] = `dep_mean'
	
	clear
	svmat2 tmat, rnames(var) names(col)
	save "$da_tmp/first_stage_immig`cond'.dta", replace
}

/***************************************************************/
/* 2.2 Run regressions - DROUGHTS */
/***************************************************************/
foreach i in $years {
	
	/* Loop over outcome sets */ 
	forval j = 1/29 {
		use $da_tmp/shockstransf_outcomes`j'_cityfe_`i', clear
		
		// Loop over the outcome variables
		foreach x in ${outcomes`j'_`i'} {
			
			// OLS regression
			qui reg `x'4 rate_mig_0110_rural4 [aw = s_n], cluster(mcm8010)
			estimates save "$da_tmp/`x'.ster", replace 
			
			// Loop over IV specifications 
			forval r = 1/6 {
				qui ivreg `x'`r' (rate_mig_0110_rural`r' = drought_dws0009) ///
				[aw = s_n], cluster(mcm8010)
				estimates save "$da_tmp/`x'_`r'.ster", replace
			}
		}
	}
}

/***************************************************************/
/* 3. Robustness */
/***************************************************************/
/* Run regressions - SPEI ABS VALUE */ 
foreach i in $years {
	
	/* Loop over outcome sets */ 
	forval j = 1/9 {
		use $da_tmp/shockstransf_outcomes`j'_cityfe_`i', clear
		
		// Loop over the outcome variables
		foreach x in ${outcomes`j'_`i'} {
			
			// OLS regression
			qui reg `x'4 rate_mig_0110_rural4 [aw = s_n], cluster(mcm8010)
			estimates save "$da_tmp/`x'_w.ster", replace 
			
			// Loop over IV specifications 
			forval r = 1/6 {
				qui ivreg `x'`r' (rate_mig_0110_rural`r' = abs_spei) ///
				[aw = s_n], cluster(mcm8010)
				estimates save "$da_tmp/`x'_`r'_w.ster", replace
			}
		}
	}
}

/* Run regressions - Clustering at the mesoregion level */
foreach i in $years {
	
	/* Loop over outcome sets */ 
	forval j = 1/9 {
		use $da_tmp/shockstransf_outcomes`j'_cityfe_`i', clear
		
		// Loop over the outcome variables
		foreach x in ${outcomes`j'_`i'} {
			
			// OLS regression
			qui reg `x'4 rate_mig_0110_rural4 [aw = s_n], cluster(mcme8010)
			estimates save "$da_tmp/`x'_meso.ster", replace 
			
			// Loop over IV specifications
			forval r = 1/6 {
				qui ivreg `x'`r' (rate_mig_0110_rural`r' = drought_dws0009) ///
				[aw = s_n], cluster(mcme8010)
				estimates save "$da_tmp/`x'_`r'_meso.ster", replace 
			}
		}		
	}
}

/***************************************************************/
/* 4. Control of commuting by housing quality */
/***************************************************************/
forval j = 6/8 {
	use $da_tmp/shockstransf_outcomes`j'_cityfe_9110, clear
	
	// Loop over the outcome variables
	foreach x in ${outcomes`j'_9110} {
		
		// OLS regression
		qui reg `x'4 rate_mig_0110_rural4 [aw = s_n], cluster(mcm8010)
		estimates save "$da_tmp/`x'_com.ster", replace 
		
		// Loop over IV specifications 
		forval r = 7/9 {
			qui ivreg `x'`r' (rate_mig_0110_rural`r' = drought_dws0009) ///
			[aw = s_n], cluster(mcm8010)
			estimates save "$da_tmp/`x'_`r'_com.ster", replace
		}
	}
}

/***************************************************************/
/* 5. Emigration effect */
/***************************************************************/
use $db_out/shift_share_db_cities.dta, clear

keep if year == 2010

rename expulsion expulsion_0010

keep mcarp8010 expulsion_0010

merge 1:m mcarp8010 using $db_out/shift_share_db_cities.dta

keep if year == 2000

/* Transform to shock-level variables */ 
ssaggregate expulsion_0010 rate_mig_0110_rural, ///
n(mca8010) s(city_rur_mig_91_sh) ///
l(mcarp8010) sfile($db_out/shift_share_db_shares.dta) ///
controls("$feff" "$controls $feff" "$controls $feff l_pop_all91" ///
"$controls $feff l_pop_all91 pop_growth0010" ///
"$controls $feff l_pop_all91 comtime_10" ///
"$controls $feff l_pop_all91 shift_8190_dws_t_rur") 

/* Bring-in the droughts variable */ 
merge 1:1 mca8010 using $db_out/shift_share_db_rural.dta
keep if _merge == 3
drop _merge
merge 1:1 mca8010 using $db_out/cluster_vars
keep if _merge == 3
drop _merge

// OLS regression
qui reg expulsion_00104 rate_mig_0110_rural1 [aw = s_n], cluster(mcm8010)
estimates save "$da_tmp/expulsion_0010_ols.ster", replace 

// Loop over IV specifications 
forval r = 1/6 {
	qui ivreg expulsion_0010`r' (rate_mig_0110_rural`r' = drought_dws0009) ///
	[aw = s_n], cluster(mcm8010)
	estimates save "$da_tmp/expulsion_0010_`r'.ster", replace
}

/***************************************************************/
/* 6. Droughts distribution */
/***************************************************************/
use $da_tmp/shockstransf_outcomes1_cityfe_9110, clear

hist drought_dws0009

graph export $da_out/graphs/appendix_figure_1.png, replace
